home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 May / EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso / softwareupdate / system / amigados / parsingcommandline / example3.c < prev    next >
C/C++ Source or Header  |  1996-10-10  |  8KB  |  218 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE)           Amiga C Club (ACC) */
  4. /* --------------------------           ------------------ */
  5. /*                                                         */
  6. /* Manual:  AmigaDOS                    Amiga C Club       */
  7. /* Chapter: Parsing Command Line        Tulevagen 22       */
  8. /* File:    Example3.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    93-03-06                                       */
  11. /* Version: 1.0                                            */
  12. /*                                                         */
  13. /*   Copyright 1993, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This example demonstrates how to parse a command line with */
  21. /* both a string and an optional value argument which require */
  22. /* a keyword. (Demonstrates the "/N" and "/K" options.)       */
  23.  
  24.  
  25.  
  26. /* Include the dos library definitions: */
  27. #include <dos/dos.h>
  28.  
  29. /* Include information about the argument parsing routine: */
  30. #include <dos/rdargs.h>
  31.  
  32. /* Now we include the necessary function prototype files:         */
  33. #include <clib/dos_protos.h>       /* General dos functions...    */
  34. #include <clib/exec_protos.h>      /* System functions...         */
  35. #include <stdio.h>                 /* Std functions [printf()...] */
  36. #include <stdlib.h>                /* Std functions [exit()...]   */
  37.  
  38.  
  39.  
  40. /* Here is our command line template. This program handles two types  */
  41. /* of command templates:                                              */
  42. /*                                                                    */
  43. /* 1. "SoundFile/A" The ReadArgs() expects one file name, else the    */
  44. /*                  function will fail. Since there is no "/M"        */
  45. /*                  option only one file name may be given.           */
  46. /*                                                                    */
  47. /* 2. V=Volume/K/N" The second type of argument is optional (no "/A"  */
  48. /*                  option. It must be a number ("/N" - Number option */
  49. /*                  is set) and preceded by the keyword "Volume" or   */
  50. /*                  "V" ("/K" - Keyword required). If a keyword is    */
  51. /*                  needed the user can either write the keyword a    */
  52. /*                  space and then the number, or the user may write  */
  53. /*                  the keyword an equal sign (=) and then the        */
  54. /*                  number. Please note that the "V=Volume" only      */
  55. /*                  means that the user can write "V" instead of the  */
  56. /*                  longer keyword "Volume", and this equal sign has  */
  57. /*                  nothing to do with the optional equal sign the    */
  58. /*                  user may write after the keyword and before the   */
  59. /*                  number. (No decimal numbers, e.g. "4.57", "1.2",  */
  60. /*                  are accepted.)                                    */
  61.  
  62. #define MY_COMMAND_LINE_TEMPLATE "SoundFile/A,V=Volume/K/N"
  63.  
  64. /* Here are some valid command lines.                                 */
  65. /*   Example3 Bird.snd                                                */
  66. /*   Example3 Bird.snd Volume=64                                      */
  67. /*   Example3 Bird.snd Volume 64                                      */
  68. /*                                                                    */
  69. /* Here are some incorrect command lines:                             */
  70. /*   Example3                     The file name is required!          */
  71. /*   Example3 Bird.snd 64         The keyword "Volume" or "V" must    */
  72. /*                                precede the number 64.              */
  73. /*   Example3 Bird.snd V=5.25     Decimal values may not be used.     */
  74.  
  75.  
  76.  
  77. /* Two command templates are used: */
  78. #define NUMBER_COMMAND_TEMPLATES 2
  79.  
  80. /* The command template numbers: (Where the result of each */
  81. /* command template can be found in the "arg_array".)      */
  82. #define SOUNDFILE_TEMPLATE  0
  83. #define VOLUME_TEMPLATE     1
  84.  
  85.  
  86.  
  87. /* Set name and version number: */
  88. UBYTE *version = "$VER: AmigaDOS/ParsingCommandLine/Example3 1.0";
  89.  
  90.  
  91.  
  92. /* Declare an external global library pointer to the Dos library: */
  93. extern struct DosLibrary *DOSBase;
  94.  
  95.  
  96.  
  97. /* Declared our own function(s): */
  98.  
  99. /* Our main function: */
  100. int main( int argc, char *argv[] );
  101.  
  102.  
  103.  
  104. /* Main function: */
  105.  
  106. int main( int argc, char *argv[] )
  107. {
  108.   /* Simple loop variable: */
  109.   int loop;
  110.  
  111.   /* A pointer to the volume value: */
  112.   LONG *volume_value;
  113.  
  114.   /* Pointer to a RDArgs structure which will automatically */
  115.   /* be created for us when we use the RDArgs() function:   */
  116.   struct RDArgs *my_rdargs;
  117.  
  118.   /* The ReadArgs() function needs an arrya of LONGs where */
  119.   /* the result of the command parsing will be placed. One */
  120.   /* LONG variable is needed for every command template.   */
  121.   LONG arg_array[ NUMBER_COMMAND_TEMPLATES ];
  122.  
  123.   /* Note! This "arg_array" must be cleared (all values set to */
  124.   /* zero) before we may use it with the ReadArgs() function.  */
  125.   /* If we declare this structure outside the main function    */
  126.   /* all values will automatically be cleared by C, but if we, */
  127.   /* as in this example, declare the array inside a function   */
  128.   /* we have to clear it manually. (If we do not clear it we   */
  129.   /* can not examine the array and see if a field is set or    */
  130.   /* not.)                                                     */
  131.  
  132.  
  133.  
  134.   /* We need dos library version 37 or higher: */
  135.   if( DOSBase->dl_lib.lib_Version < 37 )
  136.   {
  137.     /* Too old dos library! */
  138.     printf( "This program needs Dos Library V37 or higher!\n" );
  139.    
  140.     /* Exit with an error code: */ 
  141.     exit( 20 );
  142.   }
  143.  
  144.  
  145.  
  146.   /* We will now clear the "arg_array" (set all values to zero): */
  147.   for( loop = 0; loop < NUMBER_COMMAND_TEMPLATES; loop++ )
  148.     arg_array[ loop ] = 0;
  149.  
  150.  
  151.  
  152.   /* Parse the command line: */
  153.   my_rdargs = 
  154.     ReadArgs( MY_COMMAND_LINE_TEMPLATE,
  155.               arg_array,
  156.               NULL
  157.             );
  158.  
  159.   /* Have AmigaDOS successfully parsed our command line? */
  160.   if( !my_rdargs )
  161.   {
  162.     /* The command line could not be parsed! The user probably */
  163.     /* forgot to enter an argument which is required.          */
  164.     printf( "Could not parse the command line!\n" );
  165.  
  166.     /* Better luck next time... */
  167.     exit( 21 );
  168.   }
  169.  
  170.  
  171.  
  172.   /* The comand line has successfully been parsed! */
  173.   /* We can now examine the "arg_array":           */
  174.  
  175.  
  176.  
  177.   /* Print template 1, the file name: */
  178.   if( arg_array[ SOUNDFILE_TEMPLATE ] )
  179.     printf( "File name: %s\n", arg_array[ SOUNDFILE_TEMPLATE ] );
  180.  
  181.  
  182.  
  183.   /* Print templat 2, the volume. The volume was an optional */
  184.   /* argument, and we are not sure if the user has given us  */
  185.   /* a volume number or not. We must therefore check the     */
  186.   /* "arg_array" and see if the second field contains a      */
  187.   /* pointer to the volume number, or NULL (no volume is     */
  188.   /* set).                                                   */
  189.   if( arg_array[ VOLUME_TEMPLATE ] )
  190.   {
  191.     /* Get a pointer to the volume value: */
  192.     volume_value = (LONG *) arg_array[ VOLUME_TEMPLATE ];
  193.  
  194.     /* Print the volume: */
  195.     printf( "Volume: %ld\n", *volume_value );
  196.   }
  197.   else
  198.     printf( "No volume was set\n" );
  199.  
  200.  
  201.  
  202.   /* Before our program terminates we have to free the data that */
  203.   /* have been allocated when we successfully called ReadArgs(): */
  204.   FreeArgs( my_rdargs );
  205.  
  206.   /* Please note that any pointers in the "arg_array" which   */
  207.   /* pointed to some data, for example strings, may not be    */
  208.   /* used any more after you have called FreeArgs(). The data */
  209.   /* (strings etc...) have now been deallocated, and can not  */
  210.   /* be accessed any more.                                    */
  211.   
  212.  
  213.  
  214.   /* "The show must go on..." */
  215.   exit( 0 );
  216. }
  217.  
  218.